<?php
include 'connect.php';

$limit = 8;
$page = 1;

if (isset($_GET['page'])) {
	$page = $_GET['page'];
}

$where = "";

if (isset($_GET['category'])) {
	if ($_GET['category'] != "All") {
		$category = explode(",", $_GET['category']);
		$where = "";
		for ($i = 0; $i < count($category); $i++) {
			if ($where != "") {
				$where .= " OR ";
			}
			
			$where .= "category.name = '" . $category[$i] . "'";
		}
		
		$where = "WHERE " . $where;
	}
	
}

$sort = "item.id DESC";

if (isset($_GET['sort'])) {
	$field_sort = $_GET['sort'];
	if ($field_sort == "Rating") {
		$sort = "item.rating_max DESC";
	}
	
	if ($field_sort == "Popularity") {
		$sort = "item.popularity DESC";
	}
	
	if ($field_sort == "Price") {
		$sort = "item.price DESC";
	}
}

$data_item = mysql_query("SELECT item.id, title, content, price, image, popularity, category.name, rating_min, rating_max  FROM item INNER JOIN category ON item.category = category.id " . $where . "  ORDER BY " . $sort . " LIMIT ".($page-1)*$limit."," . $limit ."")  or die(mysql_error()); 

while($item = mysql_fetch_array( $data_item )) 
{
	?>
    <li class="clearafter show display" height="75">
        <div class="post-image">
            <a href="?p=bet&id=<?=$item["id"]?>"><img src="<?=$item["image"]?>" alt="post-image" title="post-image" width="105" height="69"/></a>
        </div>
        <div class="post-intro">
            <h1 class="title"><a href="?p=bet&id=<?=$item["id"]?>"><?=$item["title"] ?></a></h1>
            <span class="author">MyUserName</span>
        </div>
        <div class="post-description">
			<?=$item["content"] ?>
		</div>
        <div class="post-action">
            <div class="price">$<?=$item["price"]?></div>                                
            <div class="rating">
                <div class="action-thumb clearafter">
                    <div class="up"></div>
                    <div class="down"></div>
                </div>
                <div class="process">
                    <div class="green" style="width: <?=$item["popularity"] ?>%"></div>
                </div>                
            </div>
            <div class="category"><?=$item["name"] ?></div>
        </div>
    </li>
<?php
}
?>